home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / sharutil.2 / sharutil / sharutils-4.2 / src / encode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-24  |  2.8 KB  |  98 lines

  1. /* Handle so called `shell archives'.
  2.    Copyright (C) 1994, 1995 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software Foundation,
  16.    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18.  
  19. #include "system.h"
  20.  
  21. /* Basic one-character encoding function to make a char printing.  */
  22. #define ENCODE_BYTE(Byte) ((((Byte) + 63) & 63) + ' ' + 1)
  23.  
  24. /* Buffer size for one line of output.  */
  25. #define LINE_BUFFER_SIZE 45
  26.  
  27. /*------------------------------------------.
  28. | Output one GROUP of three bytes on FILE.  |
  29. `------------------------------------------*/
  30.  
  31. static void
  32. write_encoded_bytes (group, file)
  33.      char *group;
  34.      FILE *file;
  35. {
  36.   int c1, c2, c3, c4;
  37.  
  38.   c1 = group[0] >> 2;
  39.   c2 = ((group[0] << 4) & (3 << 4)) | ((group[1] >> 4) & 15);
  40.   c3 = ((group[1] << 2) & (15 << 2)) | ((group[2] >> 6) & 3);
  41.   c4 = group[2] & 63;
  42.   putc (ENCODE_BYTE (c1), file);
  43.   putc (ENCODE_BYTE (c2), file);
  44.   putc (ENCODE_BYTE (c3), file);
  45.   putc (ENCODE_BYTE (c4), file);
  46. }
  47.  
  48. /*--------------------------------------------------------------------.
  49. | From FILE, refill BUFFER up to BUFFER_SIZE raw bytes, returning the |
  50. | number of bytes read.                              |
  51. `--------------------------------------------------------------------*/
  52.  
  53. static int
  54. read_raw_bytes (file, buffer, buffer_size)
  55.      FILE *file;
  56.      char *buffer;
  57.      int buffer_size;
  58. {
  59.   int character;
  60.   int counter;
  61.  
  62.   for (counter = 0; counter < buffer_size; counter++)
  63.     {
  64.       character = getc (file);
  65.       if (character == EOF)
  66.     return counter;
  67.       buffer[counter] = character;
  68.     }
  69.   return buffer_size;
  70. }
  71.  
  72. /*----------------------------------------------------.
  73. | Copy INPUT file to OUTPUT file, while encoding it.  |
  74. `----------------------------------------------------*/
  75.  
  76. void
  77. copy_file_encoded (input, output)
  78.      FILE *input;
  79.      FILE *output;
  80. {
  81.   char buffer[LINE_BUFFER_SIZE];
  82.   int counter;
  83.   int number_of_bytes;
  84.  
  85.   while (1)
  86.     {
  87.       number_of_bytes = read_raw_bytes (input, buffer, LINE_BUFFER_SIZE);
  88.       putc (ENCODE_BYTE (number_of_bytes), output);
  89.  
  90.       for (counter = 0; counter < number_of_bytes; counter += 3)
  91.     write_encoded_bytes (&buffer[counter], output);
  92.       putc ('\n', output);
  93.  
  94.       if (number_of_bytes == 0)
  95.     break;
  96.     }
  97. }
  98.